package crawlerv2;

import java.util.Calendar;
import java.util.GregorianCalendar;

//CrawlerVector manipulates CrawlResultVector class instead of CrawlResult
public class CrawlerVector extends Crawler{
    private CrawlResultVector cResultVector;
    
    public CrawlerVector() {
    	cResultVector = new CrawlResultVector();
    }
    
    //this is an added function to return CrawlResultVector instead of CrawlResult
    public CrawlResultVector crawlvector(String ipAddress,int port){

        crawlCal = new GregorianCalendar(); // update the time
    	cResultVector = new CrawlResultVector(); // Create a separate result for this run
    	
        System.out.println("Crawling "+ipAddress+":"+port);
        //print out when node is being crawled
		String crawlDate = crawlCal.get(Calendar.HOUR_OF_DAY)+
        	":"+crawlCal.get(Calendar.MINUTE)+
        	" " + (crawlCal.get(Calendar.MONTH)+1) + "/" + crawlCal.get(Calendar.DAY_OF_MONTH) + "/" + crawlCal.get(Calendar.YEAR);
        System.out.println("Crawling peer at time "+crawlDate);
        String nodePeers=crawlPeers(ipAddress,port);
        //System.out.println("=================================================\nDumping nodePeers:\n" + nodePeers);
        if(nodePeers==null){
            System.out.println("Failed to crawl the peer");
            return null;
        }
        if (nodePeers.length()>0){
            parsePeersVector(nodePeers);            
        }
        else{
            System.out.println("Error : Recieved zero-length list of peers");
            return cResultVector;
        }        
        
        byte[] flist=listFiles(ipAddress,port);
        if (flist.length>0){
            parseFilesListVector(flist);            
        }
        
        cResultVector.setIPAddress(IPAddr);
        cResultVector.setHostName(HostName);
        //ask results to be created
        cResultVector.CreateResults();
        cResultVector.SetDate(crawlDate);

        return cResultVector;
    }
    
    private void parsePeersVector(String topoCrawl){
        int beginIndex;
        int endIndex;
        String upeers=new String();
        String leaves=new String();
        
        beginIndex=topoCrawl.indexOf("Peers:");
        endIndex=topoCrawl.indexOf("\n",beginIndex);
        upeers=topoCrawl.substring(beginIndex+6,endIndex);
        upeers=upeers.trim();
        
        beginIndex=topoCrawl.indexOf("Leaves:");
        endIndex=topoCrawl.indexOf("\n",beginIndex);
        leaves=topoCrawl.substring(beginIndex+7,endIndex);
        leaves=leaves.trim();        
        
        cResultVector.setUltrapeers(upeers);
        cResultVector.setLeaves(leaves);
    }
    
    private void parseFilesListVector(byte[] flist){
    	String entireMessage = new String(flist);
        int beginIndex=findEndOfHeader(flist);
        if (beginIndex<1){
            return;
        }
        String Header=new String(subBuffer(flist,0,beginIndex));
        int temp1=Header.indexOf("Server:");
        int temp2=Header.indexOf('\n',temp1);
        Header=Header.substring(temp1+8,temp2);
        Header.trim();
        temp1=Header.indexOf('/');
        if (temp1==-1) 
            temp1=Header.indexOf(' ');
        
        Header=Header.substring(0,temp1);
        cResultVector.setAgent(Header);
        flist=subBuffer(flist,beginIndex,flist.length);
        int length;
        String msg=new String();
        
        while (flist.length>0){
            flist=subBuffer(flist,19,flist.length);
            msg=new String(flist);
            length=ByteOrder.leb2int(subBuffer(flist,0,4),0);

            byte[] qhit=subBuffer(flist,4,length);
            processQueryHitVector(qhit);
            flist=subBuffer(flist,length+4,flist.length);
            if (flist==null)
                break;            
        }
    }
    
    private void processQueryHitVector(byte[] qhit){
        int beginIndex;
        int endIndex;
        int numOfFiles=qhit[0];
        String sentList=new String();
        String filesList=new String();
        String msg=new String();


        qhit=subBuffer(qhit,11,qhit.length);
        msg=new String(qhit);

        byte[] fname=new byte[512];
        int num=0;
        while (qhit.length>0 && num<numOfFiles){
            byte[] test=subBuffer(qhit,0,4);
            qhit=subBuffer(qhit,8,qhit.length);
            int x=ByteOrder.leb2int(test,0);
            
            endIndex=bufferIndexOf(qhit,'\0');
            fname=subBuffer(qhit,0,endIndex);
            qhit=subBuffer(qhit,endIndex+1,qhit.length);
            msg=new String(fname);
            filesList+=msg;
            filesList+='\0';
                        
            endIndex=bufferIndexOf(qhit,'\0');
            qhit=subBuffer(qhit,endIndex+1,qhit.length);
            num++;
            if (qhit==null)
                break;
        }
        
        cResultVector.addNumOfFiles(numOfFiles);
        cResultVector.addFilesList(filesList);
    
    }
}
